#include "myqqfacewindow.h"
#include "ui_myqqfacewindow.h"
#include "mytcpsocket.h"
#include <QLabel>
#include <QJsonDocument>
#include <QJsonObject>
#include <QDir>

MyQQFaceWindow::MyQQFaceWindow(struct st_personData user,struct st_friendData Friend,QWidget *parent) :
    QWidget(parent),
    ui(new Ui::MyQQFaceWindow)
{
    ui->setupUi(this);
    //窗口无边框
    setWindowFlag(Qt::FramelessWindowHint);
    //固定大小
    this->setFixedSize(QSize(200,100));

    ui->tableWidget->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);//根据表格自适应列宽的大小
    ui->tableWidget->verticalHeader()->setSectionResizeMode(QHeaderView::Stretch);//根据表格自适应列高的大小
    ui->tableWidget->verticalHeader()->setHidden(true); //隐藏行标题
    ui->tableWidget->horizontalHeader()->setHidden(true);//隐藏列标题
    ui->tableWidget->setSelectionMode(QAbstractItemView::SingleSelection);  //只能选中单个目标

    int rowCount = 4;   //行
    int colCount = 8;   //列
    ui->tableWidget->setRowCount(rowCount);// 设置表格行数
    ui->tableWidget->setColumnCount(colCount);// 设置表格列数

    m_user = user;
    m_friend = Friend;
    type = 53;          // 私聊表情

    //获取资源中的表情数量
    QDir *dir=new QDir("../img/biaoqing");
    QStringList filter;
    //filter<<"*.dat";
    //dir->setNameFilters(filter); //过滤文件类型
    QList<QFileInfo> *fileInfo=new QList<QFileInfo>(dir->entryInfoList(filter));
    //文件个数 会比实际多2个, . 和 ..
    int count=fileInfo->count() - 2;
    int index = 0;  //表情索引
    //4行8列
    for(int i = 0;i<rowCount;i++){
        if(index>count)
            break;
        for(int j = 0;j<colCount;j++){
            QString fpath = QString("../img/biaoqing/%1.png").arg(index++);
            //判断文件是否存在
            if(QFileInfo(fpath).exists()){
                QLabel* lab = new QLabel;
                lab->setScaledContents(true);
                lab->setPixmap(QPixmap(fpath));
                ui->tableWidget->setCellWidget(i,j,lab);
                continue;
            }
            break;
        }
    }


}

MyQQFaceWindow::MyQQFaceWindow(st_personData user, st_gData group, QWidget *parent) :
    QWidget(parent),
    ui(new Ui::MyQQFaceWindow)
{
    ui->setupUi(this);
    // 窗口无边框
    setWindowFlag(Qt::FramelessWindowHint);
    // 固定大小
    this->setFixedSize(QSize(200,100));

    ui->tableWidget->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);    // 根据表格自适应列宽的大小
    ui->tableWidget->verticalHeader()->setSectionResizeMode(QHeaderView::Stretch);      // 根据表格自适应列高的大小
    ui->tableWidget->verticalHeader()->setHidden(true);                                 // 隐藏行标题
    ui->tableWidget->horizontalHeader()->setHidden(true);                               // 隐藏列标题
    ui->tableWidget->setSelectionMode(QAbstractItemView::SingleSelection);              // 只能选中单个目标

    int rowCount = 4;                              // 行
    int colCount = 8;                              // 列
    ui->tableWidget->setRowCount(rowCount);         // 设置表格行数
    ui->tableWidget->setColumnCount(colCount);      // 设置表格列数

    m_user = user;
    m_group = group;
    type = 55;          // 群聊表情

    // 获取资源中的表情数量
    QDir *dir=new QDir("../img/biaoqing");
    QStringList filter;
    //filter<<"*.dat";
    //dir->setNameFilters(filter); // 过滤文件类型
    QList<QFileInfo> *fileInfo=new QList<QFileInfo>(dir->entryInfoList(filter));
    //文件个数 会比实际多2个, . 和 ..
    int count=fileInfo->count() - 2;
    int index = 0;  //表情索引
    //4行8列
    for(int i = 0;i<rowCount;i++){
        if(index>count)
            break;
        for(int j = 0;j<colCount;j++){
            QString fpath = QString("../img/biaoqing/%1.png").arg(index++);
            //判断文件是否存在
            if(QFileInfo(fpath).exists()){
                QLabel* lab = new QLabel;
                lab->setScaledContents(true);
                lab->setPixmap(QPixmap(fpath));
                ui->tableWidget->setCellWidget(i,j,lab);
                continue;
            }
            break;
        }
    }
}

MyQQFaceWindow::~MyQQFaceWindow()
{
    delete ui;
}

void MyQQFaceWindow::on_tableWidget_cellClicked(int row, int column)
{
    QString bq = GlobalObject::getGlobalObject()->getMapJoy()[row+column];
    //封装登录请求
    QVariantMap map;

    if(type == 53)
    {
        map["cmd"]="private_chat";
        map["recv"]=m_friend.dd_num;
    }
    else if(type == 55)
    {
        map["cmd"]="group_chat";
        map["recv"]=m_group.gid;
    }
    map["send"]=m_user.dd_num;
    map["type"]=type;
    map["content"]=bq;

    //发送表情报文
    GlobalObject::getGlobalObject()->sendRequest(map);

    //关闭表情窗口
    this->hide();
}

